package com.comtime.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.comtime.smartkilimanjaro.R;

public class MyProgressDialog extends Dialog {
	Context context;
	View view;
	TextView tv_content;
	ImageView imageView;
	
	Animation animal;
	
	 private Window window = null;
	public MyProgressDialog(Context context) {
		super(context, R.style.MyDialog);
		// TODO Auto-generated constructor stub
		this.context=context;
		
		view= LayoutInflater.from(context).inflate(R.layout.dialog_progress, null);
		imageView=(ImageView) view.findViewById(R.id.image_progress);
		tv_content=(TextView) view.findViewById(R.id.tv_content);
		
		animal=AnimationUtils.loadAnimation(context, R.anim.progress_rotate);
		animal.setInterpolator(new LinearInterpolator());
		
		imageView.setAnimation(animal);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setCanceledOnTouchOutside(false);
		
		if(tv_content.getText()==null||tv_content.getText().equals("")){
			tv_content.setVisibility(View.GONE);
		}else{
			contentString=tv_content.getText().toString();
			
		}
		
		
		this.setContentView(view);
	}
	
	
	public void setContent(CharSequence content) {
		tv_content.setText(content);
	}

	public void setContent(int contentId) {
		tv_content.setText(contentId);
	}
	

	Handler handler=new Handler();
	
	String str="",contentString="";


	

    @Override
    public void show() {
        super.show();
        WindowManager m = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display d = m.getDefaultDisplay();
        window = getWindow(); // 得到对话框
        int ints = caculateSize(-1);

        WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值
        p.width = ints;

        window.setAttributes(p);
    }

    public static final int Size_Mode_Default = -1;
    public static final int Size_Mode_Match_Window = -2;

  
    private int caculateSize(int sizeModeW) {
        WindowManager m = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        int size = 0;
        if (sizeModeW == Size_Mode_Default) {
            size = (int) (d.getWidth() * 0.97);
        } else if (sizeModeW == Size_Mode_Match_Window) {
            size = d.getWidth();
        } else {
            size = sizeModeW;
        }
        return size;
    }
    
}
